package com.chengliang.bbs.service.impl;

import com.chengliang.bbs.entry.Jie;
import com.chengliang.bbs.entry.JieStatus;
import com.chengliang.bbs.entry.User;
import com.chengliang.bbs.mapper.ExperienceMapper;
import com.chengliang.bbs.mapper.JieMapper;
import com.chengliang.bbs.mapper.JieStatusMapper;
import com.chengliang.bbs.mapper.UserMapper;
import com.chengliang.bbs.service.JieServsice;
import com.chengliang.bbs.type.FilterType;
import com.chengliang.bbs.util.StringUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: chengliang
 * @create: 2019-08-08 16:29
 */
@Service
@Slf4j
public class JieServiceImpl implements JieServsice {

    private final JieMapper jieMapper;
    private final JieStatusMapper jieStatusMapper;
    private final UserMapper userMapper;
    private final ExperienceMapper experienceMapper;

    @Autowired
    public JieServiceImpl(JieMapper jieMapper, JieStatusMapper jieStatusMapper, UserMapper userMapper, ExperienceMapper experienceMapper) {
        this.jieMapper = jieMapper;
        this.jieStatusMapper = jieStatusMapper;
        this.userMapper = userMapper;
        this.experienceMapper = experienceMapper;
    }

    @Override
    public Page<Jie> getJieListByUserId(Integer userId,Integer nowPage,Integer pageSize) {
        PageHelper.startPage(nowPage,pageSize);
        return (Page<Jie>) jieMapper.selectJieListByUserId(userId);
    }

    @Override
    public boolean modifyJieContent(Integer id, String newContent) {
        if(StringUtil.isEmpty(id)){
            return false;
        }
        return jieMapper.updateJieContextById(id,newContent);
    }

    @Override
    public User getJieUserByJieId(Integer id) {
        if(StringUtil.isEmpty(id)){
            return null;
        }
        return userMapper.selectUserByJieId(id);
    }

    @Override
    public User getJieUserByReplyId(Integer replyId) {
        return userMapper.selectUserByReplyId(replyId);
    }

    @Override
    public Jie getJieDetailById(Integer id) {
        if(StringUtil.isEmpty(id)){
            return null;
        }
        Jie jie = jieMapper.selectJieDetailById(id);
        //增加查看数量
        jieStatusMapper.addJieLookNumber(id);
        return jie;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean sendJie(Jie jie) {
        if(StringUtil.isEmpty(jie)){
            return false;
        }
        if(!experienceMapper.checkExperienceEnough(jie.getJieUser().getUserId(),jie.getRewardValue().getCode())){
            return false;
        }
        //添加帖子记录
        jieMapper.insertJie(jie);
        //初始化帖子状态
        jieStatusMapper.initJieStatus(jie.getJieId());
        //减少元气数
        experienceMapper.reduceTotalExperience(jie.getJieUser().getUserId(),jie.getRewardValue().getCode());
        return true;
    }

    @Override
    public List<Jie> getTopList() {
        return jieMapper.selectTopJieList();
    }

    @Override
    public Page<Jie> getJieList(Jie jie, FilterType filterType, Integer nowPage, Integer pageSize) {
        if(StringUtil.isEmpty(nowPage)){
            nowPage=1;
        }
        //查询条件验证
        if(jie == null){
            PageHelper.startPage(nowPage,pageSize);
            return (Page<Jie>)jieMapper.selectJieList(null);
        }
        if(StringUtil.isNotEmpty(filterType)){
            JieStatus jieStatus = new JieStatus();
            switch (filterType){
                case solved:
                    jie.setJieStatus(jieStatus.setJieEnd(true));
                    break;
                case unsolved:
                    jie.setJieStatus(jieStatus.setJieEnd(false));
                    break;
                case wonderful:
                    jie.setJieStatus(jieStatus.setJieEssence(true));
                    break;
                default:break;
            }
        }
        PageHelper.startPage(nowPage,pageSize);
        return (Page<Jie>) jieMapper.selectJieList(jie);
}
}
